<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

// connect to database
        $conn_str = 'host=dbsrv1.cdf.toronto.edu dbname=csc309g23 user=csc309g23 password=ipheid5h';
        $conn = pg_connect($conn_str);

	if($_POST['pstatus'] == 'played'){
	    $queryplaystatus = 'UPDATE games SET playstatus=TRUE WHERE gameid='.$_POST['gameid'];
	    pg_query($conn,$queryplaystatus);
	    $addfriends = 'INSERT INTO friends select * from ((select username FROM rosters WHERE gameid ='.$_POST['gameid'].') AS users CROSS JOIN (select username FROM rosters WHERE gameid='.$_POST['gameid'].') AS friending) WHERE users.username != friending.username AND users.username NOT IN (select username FROM friends WHERE friend = friending.username)';
		
	    pg_query($conn,$addfriends);
		$updaterosters = 'UPDATE rosters SET status = 4 WHERE gameid ='.$_POST['gameid'];
		pg_query($conn, $updaterosters);
		
		
        // Now need to update all players
        
	}elseif ($_POST['pstatus'] == "missed"){
	    $queryplaystatus = 'UPDATE games SET playstatus=FALSE WHERE gameid='.$_POST['gameid'];
	    pg_query($conn,$queryplaystatus);
	}
	header('Location: ' . $_SERVER['HTTP_REFERER']);
?>
